package com.ly.lw.styledo.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ly.lw.styledo.R;
import com.ly.lw.styledo.bean.CategoryTwoList;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * @author 王学玲
 *         Created by T400 on 15-7-7.
 */
public class CategoryMyGridAdapter extends AppBaseAdapter<CategoryTwoList> {

    private DisplayImageOptions options;
    private Context context;

    public CategoryMyGridAdapter(List<CategoryTwoList> list, Context context) {
        super(list, context);
        this.context = context;
        DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder();
        builder.showImageOnLoading(R.mipmap.ic_launcher);
        builder.showImageOnFail(R.mipmap.ic_launcher);
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)//设置下载的图片是否缓存在内存中
                .cacheOnDisc(true)//设置下载的图片是否缓存在SD卡中
                .build();
    }

    @Override
    public View getItemView(int position, View convertView, ViewGroup parent) {
        ViewHolder vHolder = null;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.category_two_gridview_data, parent, false);
            vHolder = new ViewHolder(convertView);
            convertView.setTag(vHolder);
        } else {
            vHolder = (ViewHolder) convertView.getTag();
        }

        CategoryTwoList bean = list.get(position);
        vHolder.cateName.setText(bean.getCate_nm());
        String imageUrl = bean.getCate_btn_img();
        ImageLoader.getInstance().displayImage(imageUrl, vHolder.imageView, options);

//        vHolder.imageView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(context, "hello~", Toast.LENGTH_SHORT).show();
//            }
//        });


        return convertView;
    }

    public static class ViewHolder {
        private ImageView imageView;
        private TextView cateName;

        public ViewHolder(View itemView) {
            imageView = (ImageView) itemView.findViewById(R.id.category_two_grid_image);
            cateName = (TextView) itemView.findViewById(R.id.category_two_grid_cateNm);
        }
    }
}
